/*=================================================
    File:	zcom_uiIMClear.c

	Module:	ui
	Author:	Mike	

=================================================*/

#include <zcom_uiIMMain.h>
#include <IMManager.h>
#include <ui.h>

//Clear: 1--inbox, 2--draft, 3--outbox
//by Mike
static void _clearDone(void * data)
{
	int index = (int) data;
	switch(index)
	{
	case 1:
		if(IM_InBox_Cleanup())
		{
			uiMenuDialogMesgTimed(
				LANG_Menu_IM_INBOX,
				langGet(LANG_Calllog_deleteall_entry_finish)
				);
		}
		break;
	case 2:
		if(IM_DraftBox_Cleanup())
		{
			uiMenuDialogMesgTimed(
				LANG_Menu_IM_DRAFT,
				langGet(LANG_Calllog_deleteall_entry_finish)
				);
		}
		break;
	case 3:
		if(IM_OutBox_Cleanup())
		{
			uiMenuDialogMesgTimed(
				LANG_Menu_IM_OUTBOX,
				langGet(LANG_Calllog_deleteall_entry_finish)
				);
		}
		break;
	default:
		return;
	}
}

static void _clearAllAsk(int index)
{
	LangIdx title = LANG_NULL;
	if(index==1)
	{
		title = LANG_Menu_IM_INBOX;
	}
	else if(index==3)
	{
		title = LANG_Menu_IM_OUTBOX;
	}
	else
	{
		title = LANG_Menu_IM_DRAFT;
	}

	uiMenuDialogMesgYes(
	    title,
	    langGet(LANG_Calllog_deleteall_entry_query),
	    _clearDone, (void *)index,
	    NULL, NULL, NULL);
}

//Clear: 1--inbox, 2--draft, 3--outbox
//by Mike
static void _clearAll(int index)
{
	int used=0, total;
	switch(index)
	{
	case 1:
		IM_InBox_GetSize(&used, &total);
		break;
	case 2:
		IM_DraftBox_GetSize(&used, &total);
		break;
	case 3:
		IM_OutBox_GetSize(&used, &total);
		break;
	default:
		return;
	}

	if(used>0)
	{
		_clearAllAsk(index);
	}
	else
	{
		char str[32] = {};
		if(index==1)
		{
			spr(str, sizeof(str), langGet(LANG_Menu_IM_Empty_Format), langGet(LANG_Menu_IM_INBOX));
			uiMenuDialogMesgTimed(
				LANG_Menu_IM_INBOX,
				str
				);
		}
		else if(index==2)
		{
			spr(str, sizeof(str), langGet(LANG_Menu_IM_Empty_Format), langGet(LANG_Menu_IM_DRAFT));
			uiMenuDialogMesgTimed(
				LANG_Menu_IM_DRAFT,
				str 
				);
			
		}
		else if(index==3)
		{
			spr(str, sizeof(str), langGet(LANG_Menu_IM_Empty_Format), langGet(LANG_Menu_IM_OUTBOX));
			uiMenuDialogMesgTimed(
				LANG_Menu_IM_OUTBOX,
				str
				);
		}

	}
}

static void _uiIMClearInboxStart(void * arg)
{
	_clearAll(1);		
}

static void _uiIMClearDraftStart(void * arg)
{
	_clearAll(2);
}

static void _uiIMClearOutboxStart(void * arg)
{
	_clearAll(3);
}

static const UIMenuNode MenuIMClearSelectNodes[] = {
	{
        .text        = {LANG_Menu_IM_INBOX}, //inbox
        .selectCB    = _uiIMClearInboxStart,
        .selectCBarg = NULL
    },

	{
        .text        = {LANG_Menu_IM_DRAFT}, //draft
        .selectCB    = _uiIMClearDraftStart,
        .selectCBarg = NULL
    },

//	{
//        .text        = {LANG_Menu_IM_OUTBOX}, //outbox
//        .selectCB    = _uiIMClearOutboxStart,
//        .selectCBarg = NULL
//    },

};


void zcom_uiIMClearStart(void * arg)
{
	UIMenuListInit menu = {};

    menu.title.textIdx = LANG_Menu_IM_title;
    menu.nNodes = sizeof(MenuIMClearSelectNodes) / sizeof(MenuIMClearSelectNodes[0]);
    menu.currNode = 0;
    menu.nDispLines = 0;
    menu.nodes = MenuIMClearSelectNodes;

	uiMenuListInit(&menu);
}
